package cc.mrbird.febs.accounting.controller;

import cc.mrbird.febs.accounting.entity.Project;
import cc.mrbird.febs.accounting.entity.Subject;
import cc.mrbird.febs.accounting.service.IProjectService;
import cc.mrbird.febs.accounting.service.ISubjectService;
import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.accounting.entity.DetailSubject;
import cc.mrbird.febs.accounting.service.IDetailSubjectService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *  Controller
 *
 * @author baoyinlei
 * @date 2021-10-11 21:37:40
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class DetailSubjectController extends BaseController {

    private final IDetailSubjectService detailSubjectService;

    private final IProjectService projectService;

    private final ISubjectService subjectService;


    @GetMapping(FebsConstant.VIEW_PREFIX + "detailSubject")
    public String detailSubjectIndex(){
        return FebsUtil.view("detailSubject/detailSubject");
    }

    @GetMapping("detailSubject")
    @ResponseBody
    @RequiresPermissions("project:list")
    public FebsResponse getAllDetailSubjects(DetailSubject detailSubject) {
        return new FebsResponse().success().data(detailSubjectService.findDetailSubjects(detailSubject));
    }

    @GetMapping("detailSubject/list")
    @ResponseBody
    @RequiresPermissions("project:list")
    public FebsResponse detailSubjectList(QueryRequest request, DetailSubject detailSubject) {
        if (detailSubject == null || detailSubject.getProjectId() == null) {
            return new FebsResponse().fail().message("请选择项目");
        }
        IPage<DetailSubject> detailSubjects = this.detailSubjectService.findDetailSubjects(request, detailSubject);
        List<DetailSubject> records = detailSubjects.getRecords();
        records.forEach(e -> {
            Project projectById = projectService.getById(e.getProjectId());
            e.setProjectName(projectById.getProjectName());
            Subject mainSubjectById = subjectService.getById(e.getSubjectId());
            e.setMainSubjectName(mainSubjectById.getSubjectName());
        });
        Map<String, Object> dataTable = getDataTable(detailSubjects);
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增DetailSubject", exceptionMessage = "新增DetailSubject失败")
    @PostMapping("detailSubject")
    @ResponseBody
    @RequiresPermissions("project:add")
    public FebsResponse addDetailSubject(@Valid DetailSubject detailSubject) {
        if (StringUtils.isBlank(detailSubject.getDetailSubjectName())
            || Objects.isNull(detailSubject.getProjectId())
                || Objects.isNull(detailSubject.getType())
            || Objects.isNull(detailSubject.getSubjectId())) {
            return new FebsResponse().fail().message("缺少参数");
        }
        this.detailSubjectService.createDetailSubject(detailSubject);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除DetailSubject", exceptionMessage = "删除DetailSubject失败")
    @GetMapping("detailSubject/delete/{detailSubjectIds}")
    @ResponseBody
    @RequiresPermissions("project:delete")
    public FebsResponse deleteDetailSubject(@NotBlank(message = "{required}") @PathVariable String detailSubjectIds) {
        this.detailSubjectService.deleteDetailSubject(detailSubjectIds);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改DetailSubject", exceptionMessage = "修改DetailSubject失败")
    @PostMapping("detailSubject/update")
    @ResponseBody
    @RequiresPermissions("project:update")
    public FebsResponse updateDetailSubject(DetailSubject detailSubject) {
        this.detailSubjectService.updateDetailSubject(detailSubject);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改DetailSubject", exceptionMessage = "导出Excel失败")
    @PostMapping("detailSubject/excel")
    @ResponseBody
    @RequiresPermissions("project:export")
    public void export(QueryRequest queryRequest, DetailSubject detailSubject, HttpServletResponse response) {
        List<DetailSubject> detailSubjects = this.detailSubjectService.findDetailSubjects(queryRequest, detailSubject).getRecords();
        ExcelKit.$Export(DetailSubject.class, response).downXlsx(detailSubjects, false);
    }
}
